package com.token.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import com.google.common.collect.Lists;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @auther: henriy
 * @Date: 2023/10/25
 */
@Configuration
public class SaTokenInterceptor implements WebMvcConfigurer {

    /**
     * 注册拦截器：可以自定义拦截器，也可以利用sa-token的拦截器，两者可以共用
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
        registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
                .addPathPatterns("/**")
                .excludePathPatterns("/user/info/login")
                .excludePathPatterns(Lists.newArrayList("/v3/api-docs/**", "/swagger-ui.html", "/swagger-ui/**", "/swagger-resources/**",
                        "/webjars/**", "/*/api-docs"));
    }

    // 方式二：LoginInterceptor为自定义拦截器
    //private final LoginInterceptor loginInterceptor;
    //
    //@Override
    //public void addInterceptors(InterceptorRegistry registry) {
    //    // excludePathPatterns("/login") 表示除了登陆与注册之外，因为登陆注册不需要登陆也可以访问
    //    registry.addInterceptor(loginInterceptor)
    //            .excludePathPatterns("/web-bff/login/login","/error")
    //            .excludePathPatterns(Lists.newArrayList("/v3/api-docs/**", "/swagger-ui.html", "/swagger-ui/**", "/swagger-resources/**",
    //                    "/webjars/**", "/*/api-docs"));
    //
    //    // 注册 Sa-Token 拦截器，打开注解式鉴权功能
    //    registry.addInterceptor(new SaInterceptor())
    //            .excludePathPatterns("/web-bff/login/login","/error")
    //            .excludePathPatterns(Lists.newArrayList("/v3/api-docs/**", "/swagger-ui.html", "/swagger-ui/**", "/swagger-resources/**",
    //                    "/webjars/**", "/*/api-docs"));
    //}


}

